package com.ticket.sass.admin.controller;


import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ticket.sass.admin.annotation.Loggable;
import com.ticket.sass.admin.annotation.Module;
import com.ticket.sass.admin.business.CommodityBusiness;
import com.ticket.sass.admin.dto.input.commodity.CommodityAddInput;
import com.ticket.sass.admin.dto.input.commodity.CommodityListInput;
import com.ticket.sass.admin.dto.input.commodity.CommodityUpdInput;
import com.ticket.sass.admin.dto.input.commodity.PublishInput;
import com.ticket.sass.admin.dto.output.commodity.CommodityInfoOutput;
import com.ticket.sass.admin.dto.output.commodity.CommodityListOutput;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.model.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ywb
 * @date 2023-04-03 13:54
 */
@RestController
@AllArgsConstructor
@Api(tags = {"卖品管理"})
@ApiSupport(order = 8)
@RequestMapping("/commodities")
public class CommodityController {
    
    final CommodityBusiness business;
    
    @GetMapping
    @ApiOperation("卖品列表")
    public ResultModel<PageModel<CommodityListOutput>> list(
        @Valid CommodityListInput input
    ) {
        return ResultModel.success(
            business.list(input)
        );
    }
    
    @ApiOperation("卖品详情")
    @GetMapping("/{id}")
    public ResultModel<CommodityInfoOutput> info(
        @PathVariable Long id
    ) {
        return ResultModel.success(
            business.info(id)
        );
    }
    
    @PostMapping
    @ApiOperation("新增卖品")
    @Loggable(module = Module.COMMODITY, desc = "新增卖品")
    public ResultModel<String> add(
        @RequestBody @Valid CommodityAddInput input
    ) {
        business.add(input);
        return ResultModel.success();
    }
    
    @PutMapping
    @ApiOperation("修改卖品")
    @Loggable(module = Module.COMMODITY, desc = "修改卖品")
    public ResultModel<String> upd(
        @RequestBody @Valid CommodityUpdInput input
    ) {
        business.upd(input);
        return ResultModel.success();
    }
    

    @ApiOperation("上架")
    @PutMapping("/publish")
    @Loggable(module = Module.COMMODITY, desc = "上架")
    public ResultModel<String> freeze(
        @RequestBody @Valid PublishInput input
    ) {
        business.publish(input);
        return ResultModel.success();
    }
    
    @ApiOperation("下架")
    @PutMapping("/unpublish")
    @Loggable(module = Module.COMMODITY, desc = "下架")
    public ResultModel<String> unfreeze(
        @RequestBody @Valid PublishInput input
    ) {
        business.unpublished(input);
        return ResultModel.success();
    }
}


